<template>
	<view>
		<view class="search-box">
			<uni-search-bar @input="input" cancelButton="none" :radius="100" placeholder="搜索" v-model="kw1"
				@confirm="add"></uni-search-bar>
			<navigator :url="'/subpkg/goods_detail/goods_detail?goods_id='+ kw">
				<button @click="add">搜索</button>
			</navigator>
		</view>
		<!-- 搜索建议列表 -->
		<view class="succ-list" v-if="searchResults.length !== 0">
			<view class="succ-item" v-for="(item, index) in searchResults" :key="index" @click="gotoDetail(item)">
				<view class="goods-name">
					{{item.goods_name}}
				</view>
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view>
		<view class="history-box" v-else>
			<!-- 标题区域 -->
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="17" @click="cleanHistory"></uni-icons>
			</view>
			<!-- 历史列表区域 -->
			<view class="history-list">
				<uni-tag v-for="(item, index) in historyList" :key="index" :text="item" :circle="true"
					@click="gotoGoodsList(item)"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 定时器的ID
				timer: null,
				// 搜索关键词
				kw: '',
				kw1: '',
				// 搜索结果列表
				searchResults: [],
				// 搜索历史的数据
				historyList: []
			};
		},
		onLoad() {
			// 调用本地存储的数据
			this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
		},
		methods: {
			// input 输入事件的处理函数
			input(e) {
				// 对 input 输入事件做防抖
				// 清除定时器
				clearTimeout(this.timer)
				// 启用定时器
				this.timer = setTimeout(() => {
					this.getSearch()
				}, 500)
			},
			add() {
				// 跳转商品列表
				uni.navigateTo({
					url: '../goods_list/goods_list?query=' + this.kw1
				})
				this.kw = this.kw1
				this.saveSearchHistory()
			},
			async getSearch() {
				if (this.kw1 === '') {
					this.searchResults = []
					return
				}
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/search', {
					query: this.kw1
				})
				if (res.meta.status !== 200) return uni.$showMsg()
				this.searchResults = res.message.goods

			},
			// 跳转商品详情
			gotoDetail(item) {
				uni.navigateTo({
					url: '../goods_detail/goods_detail?goos_id=' + item.goods_id
				})
			},
			// 添加历史记录
			saveSearchHistory() {
				this.historyList.unshift(this.kw)
				this.historyList = [...new Set(this.historyList)]
				// 调用 uni.setStorageSync() 将搜索历史记录持久化存储到本地
				uni.setStorageSync('kw', JSON.stringify(this.historyList))
			},
			// 清空历史记录
			cleanHistory() {
				// 清除 data 里面的数据
				this.historyList = []
				// 删除本地存储的数据
				uni.removeStorageSync('kw')
			},
			// 跳转商品列表
			gotoGoodsList(kw) {
				uni.navigateTo({
					url: '../goods_list/goods_list?query=' + kw
				})
			}
		}
	}
</script>

<style lang="scss">
	.search-box {
		display: flex;
		align-items: center;
		position: sticky;
		top: 0;
		z-index: 9999;
		background-color: #c00000;

		button {
			display: flex;
			align-items: center;
			font-size: 14px;
			// width: 50px;/
			height: 30px;
			border-radius: 15px;
			background-image: linear-gradient(#f26522, #ff4c31);
		}
	}

	.succ-list {
		padding: 0 10px;

		.succ-item {
			font-size: 12px;
			padding: 10px 0;
			border-bottom: 1px solid #CCCCCC;
			display: flex;
			// 副轴居中对齐
			align-items: center;
			// 两边分散对齐
			justify-content: space-between;

			.goods-name {
				// 文字不允许换行（单行）
				white-space: nowrap;
				// 溢出部分隐藏
				overflow: hidden;
				// 文字溢出后,使用...代替
				text-overflow: ellipsis;
				margin-right: 3px;
			}
		}
	}

	.history-box {
		padding: 0 10px;

		.history-title {
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 40px;
			font-size: 13px;
			border-bottom: 1px solid #efefef;
		}

		.history-list {
			display: flex;
			flex-wrap: wrap;

			.uni-tag {
				margin: 5px 5px 0 0;
			}
		}
	}
</style>
